package com.zretc.support.controller;

import com.alibaba.fastjson.JSONObject;
import com.zretc.support.constant.CommonConstant;
import com.zretc.support.entity.User;
import com.zretc.support.service.ChatSessionService;
import com.zretc.support.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class RouterController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ChatSessionService chatSessionService;

    @Autowired
    private UserService userService;

    /**
     * 根据用户手机号进入聊天室
     * @param usertel
     * @return
     */
    @GetMapping("/login/{usertel}")
    public String login(@PathVariable("usertel") String usertel){
        // 根据用户手机号获取用户信息
        User user = userService.findByUserTel(usertel);
        redisTemplate.boundValueOps(CommonConstant.USER_PREFIX + usertel).set(JSONObject.toJSONString(user));

        return "redirect:/"+usertel+"/chat";
    }

    /**
     * 聊天室页面
     * @param id
     * @return
     */
    @GetMapping("/{id}/chat")
    public String index(@PathVariable("id") String id){
        User user = chatSessionService.findById(id);
        if (user == null){
            return "redirect:/";
        }
        return "index";
    }

    @GetMapping("/")
    public String logout(){

        return "redirect:http://localhost:8088";
    }

}
